package com.bsdsma.visonManagementSystemapp.utils;

import com.alibaba.fastjson.JSONObject;
import com.bsdsma.visonManagementSystemapp.entity.User;
import com.bsdsma.visonManagementSystemapp.entity.Student;
import com.bsdsma.visonManagementSystemapp.entity.Teacher;
import com.bsdsma.visonManagementSystemapp.mapper.StudentMapper;
import com.bsdsma.visonManagementSystemapp.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 模拟Redis缓存
 */
@Component
public class RedisMockUtils {
    /**
     * 登录用户集合
     * key: 用户ID
     * value: 用户登录时生成的Token
     */
    private static final Map<Long, String> LOGIN_USER_TOKEN_MAP = new ConcurrentHashMap<>();
    /**
     * 登录用户集合
     * key: 用户ID
     * value: 用户对象
     */
    private static final Map<Long, User> LOGIN_USER_MAP = new ConcurrentHashMap<>();
    /**
     * 数据集合
     */
    private static final Map<String, Object> DATA_MAP = new ConcurrentHashMap<>();

    private static ApplicationContext applicationContext;

    @Autowired
    public void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

    /**
     * 保存用户Token
     * @param uid   用户ID
     * @param token 用户Token
     */
    public static void putUserToken(Long uid, String token){
        LOGIN_USER_TOKEN_MAP.put(uid, token);
    }

    /**
     * 将登录用户对象放到集合中
     * @param uid   用户ID
     * @param user  用户对象
     */
    public static void putUser(Long uid, User user){
        LOGIN_USER_MAP.put(uid, user);
    }

    /**
     * 获取登录用户
     * @param uid   用户ID
     */
    public static User getUser(Long uid){
        User user = LOGIN_USER_MAP.get(uid);
        if (user != null && applicationContext != null) {
            // Load school relationship for students and teachers
            if (user instanceof Student) {
                Student student = (Student) user;
                StudentMapper studentMapper = applicationContext.getBean(StudentMapper.class);
                student = studentMapper.selectByIdWithSchool(student.getId());
                return student;
            } else if (user instanceof Teacher) {
                Teacher teacher = (Teacher) user;
                TeacherMapper teacherMapper = applicationContext.getBean(TeacherMapper.class);
                teacher = teacherMapper.selectByIdWithSchool(teacher.getId());
                return teacher;
            }
        }
        return user;
    }

    /**
     * 通过用户ID获取用户Token
     * @param uid   用户ID
     * @return      用户Token 可能是空的
     */
    public static String getUserToken(Long uid){
        return LOGIN_USER_TOKEN_MAP.get(uid);
    }

    /**
     * 移除某用户Token
     * @param uid   用户ID
     */
    public static void removeUserToken(Long uid){
        LOGIN_USER_TOKEN_MAP.remove(uid);
    }

    public static <T> void dataSet(String key, T value){
        DATA_MAP.put(key, value);
    }

    public static <T> T dataGet(String key, Class<T> clazz){
        Object obj = DATA_MAP.get(key);
        if(null == obj){
            return null;
        }
        return JSONObject.parseObject(JSONObject.toJSONString(obj), clazz);
    }

    public static <T> List<T> dataGetList(String key, Class<T> clazz){
        Object obj = DATA_MAP.get(key);
        if(null == obj){
            return null;
        }
        return JSONObject.parseArray(JSONObject.toJSONString(obj), clazz);
    }
}
